#ifndef SUBJECTFILEREADER_H_INCLUDED
#define SUBJECTFILEREADER_H_INCLUDED
#include "../Entities/subject.h"
#include <vector>
#include <fstream>
#include <stdlib.h>

namespace SubjectFileReader{
    typedef std::vector<Subject> SubjectList;

    SubjectList read(const char *filename){
        SubjectList ret;
        std::ifstream file(filename, std::ios::in);
        if(!file.is_open())
            throw "file cannot be loaded.";

        int id = 1;
        while(!file.eof()){
            char line[500];
            file.getline(line, 500);

            StringTokenizer tokenizer(line,"%");

            if(!tokenizer.hasMoreTokens())
                throw "invalid file format.";
            std::string code = tokenizer.nexToken();

            if(!tokenizer.hasMoreTokens())
                throw "invalid file format.";
            std::string desc = tokenizer.nexToken();

            if(!tokenizer.hasMoreTokens())
                throw "invalid file format.";

            try{
                int load = atoi(tokenizer.nexToken().c_str());

                Subject a(code.c_str(), desc.c_str(), load, id++);
                ret.push_back(a);
            } catch(...){
                throw "invalid file format.";
            }
        }

        return ret;
    }
}


#endif // SUBJECTFILEREADER_H_INCLUDED
